import Vue from 'vue'
import axios from 'axios'
import VueAxios from 'vue-axios'
import qs from 'qs'
import base64 from 'base-64'
window.qs = qs
window.base64 = base64
import VueAuth from '@websanova/vue-auth'
import NProgress from 'vue-nprogress'
import { sync } from 'vuex-router-sync'
import App from './App.vue'
import router from './router'
import store from './store'
import * as filters from './filters'
import { TOGGLE_SIDEBAR } from 'vuex-store/mutation-types'
window.Clipboard = require('clipboard')

Vue.router = router
axios.defaults.timeout = 5000
axios.defaults.headers.common['Authorization'] = 'Bearer ' + window.localStorage.getItem('Authorization')
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
// 配置接口地址
axios.defaults.baseURL = 'http://118.242.0.43:9090/'

window.HTTPS = 'http://118.242.0.43:9090/'

//  返回状态判断(添加响应拦截器)
axios.interceptors.response.use((res) => {
  return res
}, (error) => {
  if((!error.response)||error.response.status == '401'){
    window.location.href = '/#/login'
  }else{
    return Promise.reject(error)

  }
})
Vue.use(VueAxios, axios)
Vue.use(VueAuth, {
  auth: {
    request: function (req, token) {
    },
    response: function (res) {
      // Get Token from response body
      return res.data
    }
  },
  http: require('@websanova/vue-auth/drivers/http/axios.1.x.js'),
  router: require('@websanova/vue-auth/drivers/router/vue-router.2.x.js'),
  loginData: { url: 'http://121.199.52.14:8183/pms/hotel/list', fetchUser: false },
  refreshData: { enabled: false }
})

Vue.use(NProgress)
Vue.use(qs)

// Enable devtools
Vue.config.devtools = true

sync(store, router)

const nprogress = new NProgress({ parent: '.nprogress-container' })

const { state } = store

router.beforeEach((route, redirect, next) => {
  if (state.app.device.isMobile && state.app.sidebar.opened) {
  store.commit(TOGGLE_SIDEBAR, false)
}
next()
})

Object.keys(filters).forEach(key => {
  Vue.filter(key, filters[key])
})

const app = new Vue(Object.assign({
  router,
  store,
  nprogress
},App))

export { app, router, store }
